<?php

/**
 * This is the model class for table "lths_producttype".
 *
 * The followings are the available columns in table 'lths_producttype':
 * @property integer $producttype_code
 * @property string $producttype_name
 * @property string $remark
 * @property string $show_order
 * @property string $create_time
 * @property string $update_time
 *
 * The followings are the available model relations:
 * @property Product[] $products
 */
class Producttype extends ParamModel
{
	
	public $keyname='producttype_code';
	public $displayname='producttype_name';
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Producttype the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'lths_producttype';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('producttype_code', 'numerical', 'integerOnly'=>true),
			array('producttype_name', 'length', 'max'=>50),
			array('remark', 'length', 'max'=>200),
			array('show_order, create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('producttype_code, producttype_name, remark, show_order, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			//'products' => array(self::HAS_MANY, 'Product', 'producttype_code'),
			'products' => array(self::MANY_MANY, 'Product', 'lths_producttype_product(producttype_code, product_id)','order'=>'products.product_name'),
		);
	}
	
	public function getUsertypeJson()
	{
		$results = array();
		$usertypeModels = BUsertype::model()->findAll();
		foreach($usertypeModels as $usertype)
		{
			$results[$usertype->usertype_code] = array();
			foreach($usertype->producttypes as $producttype)
			{
				$results[$usertype->usertype_code][$producttype->producttype_code] = $producttype->producttype_name;
			}
		}
		return CJSON::encode($results);
	}
	
	public function getListDataByUserType($userType)
	{
		$results = array();
		$userTypeModel = BUsertype::model()->findByPk($userType,array('with'=>'producttypes',));
		if(isset($userTypeModel))
		{
			$models = $userTypeModel->producttypes;
			foreach ($models as $key => $value) {
	    	$results[$value[$this->keyname]] = $value[$this->displayname];
	        }
		}
	    return $results;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'producttype_code' => 'Producttype Code',
			'producttype_name' => 'Producttype Name',
			'remark' => 'Remark',
			'show_order' => 'showOrder',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('producttype_code',$this->producttype_code);
		$criteria->compare('producttype_name',$this->producttype_name,true);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}